package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.List;

import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import org.springframework.stereotype.Service;

@Service
public class CommonService {
	/**
	 * 根据站点获取运营单位
	 * @param wwtpid 污水处理厂ID
	 * @return 运营单位对象
	 */
	public String getOperationUnitByStaionId(String station_id){
		String sql = "select ou.NAME from BC_SITE_INFO ms"
				+ " left join BC_ENTERPRISE_ATTR pwa on ms.wwtp_id = pwa.exp_no"
				+ " left join operations_unit ou on pwa.operator = ou.guid"
				+ " where ms.code = @station_id  and rownum = 1";
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("station_id",station_id));
		Object obj = DataUtil.getScale(sql, parms);
		if(obj!=null)
			return obj.toString();
		return null;
	}
	/**
	 * 根据污水处理厂获取运营单位
	 * @param wwtpid 污水处理厂ID
	 * @return 运营单位对象
	 */
	public String getOperationUnit(String wwtpid){
		String sql = "select ou.NAME from BC_ENTERPRISE_ATTR pwa left join operations_unit ou on pwa.operator=ou.guid where pwa.exp_no=@WWTPID and rownum=1";
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("WWTPID",wwtpid));
		Object obj = DataUtil.getScale(sql, parms);
		if(obj!=null)
			return obj.toString();
		return null;
	}
	
	/**
	 * 根据污水厂id和排口类型获取站点对象，包括站点id和站点名称属性
	 * @param wwtpid
	 * @param stationType
	 * @return
	 */
	public RowBase getStation(String wwtpid,String flowType)
	{
		String getStationIdSql = "select ms.code,ms.name from BC_SITE_INFO ms where ms.wwtp_id = @WSCLC_ID and ms.flow_type = @FLOW_TYPE and rownum=1";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		GlobalParameter wwtpIdParm = new GlobalParameter("WSCLC_ID", wwtpid);
		GlobalParameter stationTypeParm = new GlobalParameter("FLOW_TYPE", flowType);
		parms.add(wwtpIdParm);
		parms.add(stationTypeParm);
		List<RowBase> list = DataUtil.getSelectRows(getStationIdSql, parms);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 根据污水厂id和排口类型获取 站点的运维组信息
	 * @param wwtpid
	 * @param stationType
	 * @return
	 */
	public RowBase getStationGroupInfo(String wwtpid,String flowType)
	{
		RowBase station = getStation(wwtpid,flowType);
		if(station!=null){
			String station_id = station.getSafeString("CODE");
			return getStationGroupInfo(station_id);
		}
		return null;
	}
	
	/**
	 * 根据站点id获取 站点的运维组信息
	 * @param wwtpid
	 * @param stationType
	 * @return
	 */
	public RowBase getStationGroupInfo(String station_id)
	{
		String sql = "select ogsa.station_id,kk.* from operation_group_station_asso ogsa left join (select og1.group_name,tt1.group_id,tt1.group_leader,tt1.s_userid from OC_GROUP og1 left join (select distinct o1.group_id,o1.group_head,su1.c_userealname as group_leader,su1.s_userid from OC_GROUP_ASSO o1"
				+" left join safe_user su1 on o1.user_id=su1.s_userid where o1.group_head='1') tt1 on og1.guid = tt1.group_id) kk on ogsa.operation_group_id=kk.GROUP_ID"
				+" where ogsa.station_id=@station_id";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		GlobalParameter wwtpIdParm = new GlobalParameter("station_id", station_id);
		parms.add(wwtpIdParm);
		List<RowBase> list = DataUtil.getSelectRows(sql, parms);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
}
